package common;

import java.util.HashMap;
import java.util.Map;

public class NameTagUtils
{
    private Map<String, Integer> idMap;
    private Map<String, Integer> nameMap;

    public NameTagUtils()
    {
        idMap = new HashMap<>();
        nameMap = new HashMap<>();
    }

    public String getNameHitFix(String name)
    {
        String resName;
        if (nameMap.containsKey(name))
        {
            int number = nameMap.get(name);
            resName = numberSuffixFormat(name, number);
            nameMap.computeIfPresent(name, (k, v) -> v + 1);
        }
        else
        {
            resName = name;
            nameMap.put(name, 2);
        }
        return resName;
    }

    public String getIdHitFix(String id)
    {
        String resId;
        if (idMap.containsKey(id))
        {
            int number = idMap.get(id);
            resId = numberSuffixFormat(id, number);
            idMap.computeIfPresent(id, (k, v) -> v + 1);
        }
        else
        {
            resId = id;
            idMap.put(id, 2);
        }
        return resId;
    }

    private String numberSuffixFormat(String s, int n)
    {
        return String.format("%s_%d", s, n);
    }

    public String queryLastNameHitFix(String name)
    {
        if (!nameMap.containsKey(name))
            return name;
        int number = nameMap.get(name);
        if (number == 2)
            return name;
        return numberSuffixFormat(name, number - 1);
    }

    public String queryLastIdHitFix(String id)
    {
        if (!idMap.containsKey(id))
            return id;
        int number = idMap.get(id);
        if (number == 2)
            return id;
        return numberSuffixFormat(id, number - 1);
    }
}
